Plotting in Julia

We will use the PyPlot package to plot with Julia. This notebook has a few examples to get you started. The PyPlot.jl site has excellent documentation for plotting.

Loading the PyPlot module may take a few seconds.

In general, all of the arguments, including keyword arguments, are exactly the same as in Python. (With minor translations, of course, e.g. Julia uses true and nothing instead of Python's True and None.)

The full matplotlib.pyplot API is far too extensive to describe here; see the matplotlib.pyplot documentation for more information. The Matplotlib version number is returned by PyPlot.version.


In [1]:
using PyPlot

In [2]:
x = linspace(0,2*pi,1000); y = sin(3*x + 4*cos(2*x));
plot(x, y, color="red", linewidth=2.0, linestyle="--")
title("A sinusoidally modulated sinusoid")


Out[2]:
PyObject <matplotlib.text.Text object at 0x7f097234f510>

In [3]:
# Draw (x, y) points
figure(figsize=(5, 5))
θ = collect(0:0.1:2π)
plot(0,0,"b.")
plot(cos(θ), sin(θ), "r.")
plot(0.5cos(θ), 0.5sin(θ), "g.")


WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
WARNING: Base.writemime is deprecated.
  likely near /opt/julia_packages/.julia/v0.5/IJulia/src/kernel.jl:31
in show at /opt/julia_packages/.julia/v0.5/PyCall/src/PyCall.jl
Out[3]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f0972136790>

In [8]:
# Draw a histogram

y = randn(10^6)
plt[:hist](y, 50)     # We use plt.hist, because it conflicts with the built-in hist


Out[8]:
([1.0,0.0,0.0,2.0,6.0,20.0,37.0,98.0,171.0,376.0  …  1084.0,627.0,304.0,160.0,87.0,44.0,19.0,3.0,3.0,1.0],[-5.19091,-4.99025,-4.78958,-4.58891,-4.38825,-4.18758,-3.98691,-3.78624,-3.58558,-3.38491  …  3.03644,3.2371,3.43777,3.63844,3.83911,4.03977,4.24044,4.44111,4.64177,4.84244],Any[PyObject <matplotlib.patches.Rectangle object at 0x7f096e543f50>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e54d5d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e54dad0>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e54dfd0>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e55c690>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e55cd10>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e56a3d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e56aa50>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e56af50>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e576790>  …  PyObject <matplotlib.patches.Rectangle object at 0x7f096e487610>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e487c90>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e494350>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e4949d0>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e494ed0>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e4a1710>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e4a1d90>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e4ad450>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e4adad0>,PyObject <matplotlib.patches.Rectangle object at 0x7f096e4adfd0>])

In [44]:
# Draw a stacked bar chart

N = 5
menMeans   = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd     = (2, 3, 4, 1, 2)
womenStd   = (3, 5, 2, 3, 3)
ind = 1:N    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = bar(ind, menMeans, width, color="r", yerr=menStd)
p2 = bar(ind, womenMeans, width, bottom=menMeans, color="b", yerr=womenStd)

ylabel("Scores")
title("Scores by group and gender")
xticks(ind+width/2., ("G1", "G2", "G3", "G4", "G5") )
legend( (p1[1], p2[1]), ("Men", "Women") )


Out[44]:
PyObject <matplotlib.legend.Legend object at 0x7f096d4e7890>

In [6]:
# Plot a random surface

surf(rand(30,40))


Out[6]:
PyObject <mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x7f096e75f6d0>